﻿/*
 * Copyright (c) 2010, Sergey Loktin (mailto://shadowconsp@gmail.com)
 * Licensed under The MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using MoonyPlugin;

namespace MoonyDesk
{
    /// <summary>
    /// Loaded into system plugin
    /// </summary>
    public class LoadedPlugin
    {
        IPlugin _plugin;
        Dictionary<string, object> _defaultValues;

        public DesktopWindow MainWindow { get; private set; }
        public string Name { get { return _plugin.Name; } }
        public bool Active { get; private set; }

        public LoadedPlugin(IPlugin Plugin, Dictionary<string, object> defaulValues)
        {
            _plugin = Plugin;
            Active = false;
            _defaultValues = defaulValues;
        }

        /// <summary>
        /// Activate plugin, create plugin's main window, load main window settings
        /// </summary>
        public void Activate()
        {
            _plugin.Initialize();
            MainWindow = new DesktopWindow();
            MainWindow.Title = _plugin.Name;
            MainWindow.viewBox.Child = _plugin.DesktopElement;
            MainWindow.Show();
            Active = true;

            //load settings
            if (Properties.Settings.Default.xPositions[this.Name] == null)
            {
                if (_defaultValues.ContainsKey("DefaultLeft"))
                    MainWindow.Left = Convert.ToDouble(_defaultValues["DefaultLeft"]);
            }
            else
                MainWindow.Left = double.Parse(Properties.Settings.Default.xPositions[this.Name]);
            if (Properties.Settings.Default.yPositions[this.Name] == null)
            {
                if (_defaultValues.ContainsKey("DefaultTop"))
                    MainWindow.Top = Convert.ToDouble(_defaultValues["DefaultTop"]);
            }
            else
                MainWindow.Top = double.Parse(Properties.Settings.Default.yPositions[this.Name]);
            if (Properties.Settings.Default.movables[this.Name] == null)
            {
                if (_defaultValues.ContainsKey("DefaultMovable"))
                    MainWindow.Movable = Convert.ToBoolean(_defaultValues["DefaultMovable"]);
            }
            else
                MainWindow.Movable = bool.Parse(Properties.Settings.Default.movables[this.Name]);
            if (Properties.Settings.Default.topmosts[this.Name] == null)
            {
                if (_defaultValues.ContainsKey("DefaultTopmost"))
                    MainWindow.Topmost2 = Convert.ToBoolean(_defaultValues["DefaultTopmost"]);
            }
            else
                MainWindow.Topmost2 = bool.Parse(Properties.Settings.Default.topmosts[this.Name]);
            if (Properties.Settings.Default.sizes[this.Name] == null)
            {
                if (_defaultValues.ContainsKey("DefaultSize"))
                    MainWindow.viewBox.Width = Convert.ToDouble(_defaultValues["DefaultSize"]);
            }
            else
                MainWindow.viewBox.Width = double.Parse(Properties.Settings.Default.sizes[this.Name]);
            if (Properties.Settings.Default.angles[this.Name] == null)
            {
                if (_defaultValues.ContainsKey("DefaultAngle"))
                    MainWindow.rotate.Angle = Convert.ToDouble(_defaultValues["DefaultAngle"]);
            }
            else
                MainWindow.rotate.Angle = double.Parse(Properties.Settings.Default.angles[this.Name]);
        }


        /// <summary>
        /// Diactivate plugin, save main window settings, close plugin's main window
        /// </summary>
        public void Diactivate()
        {
            //save settings
            Properties.Settings.Default.xPositions[this.Name] = MainWindow.Left.ToString();
            Properties.Settings.Default.yPositions[this.Name] = MainWindow.Top.ToString();
            Properties.Settings.Default.movables[this.Name] = MainWindow.Movable.ToString();
            Properties.Settings.Default.topmosts[this.Name] = MainWindow.Topmost2.ToString();
            Properties.Settings.Default.sizes[this.Name] = MainWindow.viewBox.Width.ToString();
            Properties.Settings.Default.angles[this.Name] = MainWindow.rotate.Angle.ToString();

            _plugin.Dispose();
            MainWindow.Close();
            MainWindow = null;
            Active = false;
        }

        public List<SettingsTab> GetNewSettingsTabs()
        {
            return _plugin.GetNewSettingsTabs();
        }
    }
}
